const app = getApp()
const util= require('../../utils/util.js')

Page({
    data: {
        username: '', 
        password: '',
        api: app.globalData.api
    },
    username(e){
        this.data.username=e.detail.value
    },
    password(e){
        this.data.password=e.detail.value
    },
    onLoad:function(){
      
    },
    register() {
      
        
        if(this.data.username==''||!this.data.username){
        wx.showToast({
            icon:'none',
            title: '用户名不能为空',
        })
        }else if(this.data.password==''||!this.data.password){
        wx.showToast({
            icon:'none',
            title: '密码不能为空',
        })}else{
            var user = {
              username:this.data.username,
              password:this.data.password
            }
            //已经输入完成用户名和密码
            //提交到后端
            wx.request({
              url: this.data.api+'/wx/adduser',
              data: user,
              method: 'POST',
              header: {
                  'content-Type': 'application/json'
              },
              success:function(res){
                console.log(res)
                if(res.data.code=='200'){
                  wx.showToast({
                    title: '注册成功',
                    icon: 'success',
                    duration: 2500,
                    success:function(){
                      setTimeout(function(){
                        util.redirect('login')
                      },2500)
                    }
                  })
                }else{
                  wx.showToast({
                    title: res.data.msg,
                    icon: 'error',
                    duration: 2500
                  })
                }    
              }  
            }) 
        }
    },  
})
